/*  
  -- The Lime II Project -- 

  A tuplespaces-based middleware for coordinating agents and mobile hosts.
  Copyright (C) 2005.
  Gian Pietro Picco, Amy L. Murphy, Lorenzo Bellini.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received this copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
*/

package testing;
import lime2.*;
import lights.adapters.*;
import java.util.Random;

/**
 * A simple agent testing whether it matters the order in which a strong reaction and a (local) weak one are installed:
 * if both of them are enabled which one will be the first to fire, the strong one or the weak one ? 
 * <br>
 * The design of this agent requires to load it in isolation wrt other agents. Also we require the LimeTupleSpace to 
 * be empty of tuples and reactions. 
 * <br>
 * The scenario is very interesting: please read the comments in the code.
 *  
 * @author Gian Pietro Picco
 * @author Amy Murphy
 * @author Lorenzo Bellini
 */

public class ReactPrec implements Runnable, ReactionListener
{
 //	identification number	
 private int id = new Random().nextInt(1000);			
	
 // private data members 	
 private LimeTupleSpace lts = LimeTupleSpace.get("testing");
 private LimeTemplate te = new LimeTemplate(Location.HERE, Location.UNSPECIFIED, new Tuple().add(new Field().setType(String.class)));
 private String msg = null;
 
 /**
  * Constructor method. <code>ReactPrecedence(String)</code> "destroys" the implicit empty constructor, so we have to 
  * explicitly define a (public) empty constructor (see class Launcher).
  */
 public ReactPrec()
 {
  // empty constructor	
 }
 
 /**
  * Constructor method. Non empty.
  * 
  * @param msg message
  */
 private ReactPrec(String msg)
 {
  this.msg = msg;	
 }
 
 /**
  * Reactive method.
  * 
  * @param re ReactionEvent
  */
 public void reactsToOut(ReactionEvent re)
 {	
  // consumes tuple	
  lts.inp(te);
  System.out.println(msg); 
 }
 
 /**
  * Run method.
  */
 public void run()
 {
  // agent starts
  System.out.println("ReactPrec " + id + ": started");     	
 	
  // creates two identical once reactions
  Reaction rs = new Reaction(Reaction.ONCE, te, new ReactPrec("ReactPrec " + id + ": strong reaction fired"));
  Reaction rw = new Reaction(Reaction.ONCE, te, new ReactPrec("ReactPrec " + id + ": weak reaction fired"));
  
  // FIRST CASE
  
  // installs at first the strong reaction and then installs the weak one
  lts.addStrongReaction(new Reaction[] {rs});
  lts.addWeakReaction(new Reaction[] {rw});
  System.out.println("ReactPrec " + id + ": installed strong reaction, then weak reaction");
  // outs two tuples
  lts.out(new Tuple().add(new Field().setValue("hello")));
  lts.out(new Tuple().add(new Field().setValue("hello")));
  // both reactions fire
  
  // SECOND CASE
  
  // installs at first the weak reaction and then installs the strong one 
  lts.addWeakReaction(new Reaction[] {rw});
  lts.addStrongReaction(new Reaction[] {rs});
  System.out.println("ReactPrec " + id + ": installed weak reaction, then strong reaction");
  // outs one tuple
  lts.out(new Tuple().add(new Field().setValue("hello")));
  // both reactions fire
  
  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  // why in this second case just one tuple makes both reactions fire ?                                          // 
  // In the first case we need two tuples because the strong reaction is the first one to fire; it consumes the  //
  // first tuple. Then the weak reaction fires and it consumes the second tuple.                                 //
  // But in the second case it is the weak reaction that fires first: but the system listener (strong part) just // 
  // posts a WeakOp to the events queue of the LocalOpMgr. Then the strong reaction fires (remember: Reactor     // 
  // iterates on a FIFO linked list) and it consumes the tuple. The reactor becomes quiet; when the LocalOpMgr   //
  // extracts and processes the next op (the WeakOp) it fires the weak part of the weak reaction, but the tuple  //
  // has already been consumed.                                                                                  //
  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  
  // agent dies
  System.out.println("ReactPrec " + id + ": dies");     
 }

@Override
public void reactsToTake(ReactionEvent re) {
	// TODO Auto-generated method stub
	
}
 
}
